package com.smartlbs.baseframe.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.output.FileWriterWithEncoding;
import org.dom4j.Branch;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smartlbs.entity.OtherMatch.Match;

/**
 * 属性文件读取类
 * 
 * 
 * 
 * 
 *
 */
public class ConfigFileUtils {

	public static String PROPERTY_PATH = "configs/setting/info.properties";
	public static String LOG_PATH = "configs/setting/ips.log";

	/**
	 * 获取属性字符串
	 * 
	 * 
	 * @param keyName
	 *            键名称
	 * @return
	 */
	public static String getPropertyString(String keyName) {
		/* config.properties文件路径 */
		try {
			Properties pp = new Properties();
			String filePath = ConfigFileUtils.class.getResource("/").getPath() + PROPERTY_PATH;
			InputStream	in =new FileInputStream(filePath);
			//System.out.println(filePath);
			//InputStream in = ConfigFileUtils.class.getClassLoader().getResourceAsStream(PROPERTY_PATH);
			pp.load(in);

			String valuestr = pp.getProperty(keyName);

			return valuestr;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 设置属性字符串
	 * 
	 * 
	 * @param keyName
	 *            键名称
	 * @return
	 */
	public static void setPropertyString(String key, String value, String comment) {
		try {
			//
			Properties pp = new Properties();
			//
			String filePath = ConfigFileUtils.class.getResource("/").getPath() + PROPERTY_PATH;
			System.out.println(filePath);
			//
			InputStream in = new FileInputStream(filePath);
			pp.load(in);
			//
			FileOutputStream oFile = new FileOutputStream(filePath);

			pp.setProperty(key, value);
			// 这里的第二个参数是用来写注释的。在Proterties中的注释是"#"
			pp.store(oFile, comment);
			oFile.flush();
			oFile.close();
			System.out.println(filePath);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public static void setIpslog(String logs) {
		FileOutputStream fos = null;
		
		try {
			getIplog();
			//String filePath = ConfigFileUtils.class.getResource("/").getPath() + LOG_PATH;
			String classpath = ConfigFileUtils.class.getResource("/").getPath();
			String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");
			String filePath=webappRoot+"/views/file/ips.log";
			
			fos = new FileOutputStream(filePath,  true);
			OutputStreamWriter fileWriter= new OutputStreamWriter(fos, "UTF-8");
			System.out.println(filePath);

			if (!StringUtils.isEmpty(logs)) {

				fileWriter.write(logs);
				fileWriter.write("\r\n");
			}
			fileWriter.flush();
			fileWriter.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	public static void getIplog() {
		InputStream fileWriter = null;
	//	String filePath = ConfigFileUtils.class.getResource("/").getPath() + LOG_PATH;
		String classpath = ConfigFileUtils.class.getResource("/").getPath();
		String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");
		String filePath=webappRoot+"/views/file/ips.log";
		System.out.println(filePath);
		
		try {
			fileWriter = new FileInputStream(filePath);
		
			InputStreamReader isr = new InputStreamReader(fileWriter);
			BufferedReader br = new BufferedReader(isr);
			int line = 0;
		
			while (br.readLine() != null) {
				line++;
			}
			if (line > 99) {
				clearIpslog();
			}
			br.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Map<String, Object> getBetJson() {
		InputStream fileWriter = null;
		String json ="";
		String classpath = ConfigFileUtils.class.getResource("/").getPath();
		String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");
		String filePath=webappRoot+"/views/file/bet.json";
		try {
			fileWriter = new FileInputStream(filePath);
		
			InputStreamReader isr = new InputStreamReader(fileWriter,"UTF-8");
			BufferedReader br = new BufferedReader(isr);
			String readLine = br.readLine();
			json=json+readLine;
			br.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String, Object> valueMap = new HashMap<String, Object>();
		if(!StringUtils.isEmpty(json)){
			JSONObject jsonObject = JSONObject.parseObject(json);
			valueMap.putAll(jsonObject);
		}
		return valueMap;
	}

	public static void clearIpslog() throws Exception {
		String classpath = ConfigFileUtils.class.getResource("/").getPath();
		String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");
		String filePath=webappRoot+"/views/file/ips.log";
		File file = new File(filePath);

		if (!file.exists()) {
			file.createNewFile();
		}

		FileWriter fileWriter = new FileWriter(file);
		fileWriter.write("");

		fileWriter.flush();

		fileWriter.close();

	}
	/** 更新赛事词典 excel
	 * @param newfile
	 * @return
	 */
	public static int mobileExcel(MultipartFile newfile){
		int ret=0;
		String name = newfile.getOriginalFilename();
		String ext = name.substring(name.lastIndexOf("."));
		if(!ext.equals(".xlsx")){
			return -1;
		}
		String filePath =  ConfigFileUtils.class.getResource("/").getPath() + "configs/excel/gameinfo.xlsx";
		File oldFile = new File(filePath);
		
			try {
				InputStream input = newfile.getInputStream();
				BufferedInputStream inBuff=new BufferedInputStream(input);
				FileOutputStream output = new FileOutputStream(oldFile);
				BufferedOutputStream outBuff=new BufferedOutputStream(output);
				byte[] b = new byte[1024 * 5];
				int len;
				while ((len =inBuff.read(b)) != -1) {
				    outBuff.write(b, 0, len);
				}
				outBuff.flush();
				inBuff.close();
				outBuff.close();
				output.close();
				input.close();
				ret =1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		return ret;
		
	}

}